(function(){
	//定一个共用的前缀
	var api = 'http://h6.duchengjiu.top/shop/';
	
	oTable = document.getElementById('table');
	var token = localStorage.getItem('token');
	$.get(api+'api_cart.php',{'token':token},function(data){
		var obj = data.data;
		console.log(obj);
		var th = `
			<tr>
				<th width="100"><input type='checkbox' class='allBtn' />全选</th>
				<th width="300">良品</th>
				<th width="300">数量</th>
				<th width="100">单价</th>
				<th width="100">小计</th>
				<th width="100">操作</th>
			</tr>
		`;
		
		var trStr ='';
		//遍历数据
		for(var i=0; i<obj.length;i++){
			var xiaoji = obj[i].goods_number*obj[i].goods_price+'.00'
			trStr +=`
			<tr>
				<td>
					<input type='checkbox' class='check'/>
					<a  href='javascript:;'><img src='${obj[i].goods_thumb}' alt=''/></a></td>
				<td>${obj[i].goods_name}</td>
				<td>
					<span id='minus' data-id='${obj[i].goods_id}'>-</span>
					<input type='text' id='number' value='${obj[i].goods_number}'/>
					<span id='add' data-id='${obj[i].goods_id}'>+</span>
				</td>
				<td>${obj[i].goods_price}</td>
				<td>${xiaoji}</td>
				<td>
					<a href='javascript:;' class='delete' data-id='${obj[i].goods_id}'>删除</a>
				</td>
			</tr>
			`;
		};
		
		oTable.innerHTML = th+trStr;
	});
	//用事件对象去做交互
	(function(){
		//定义全局变量
		var lock = false;//定义一个锁
		oTable.onclick = function(event){
			var event = event || window.event;
			//点击减号
			if(event.target.innerText == '-'){
				var oInput = event.target.nextElementSibling;
				var oInputV = parseInt(oInput.value);
				oInputV--;
				oInputV = oInputV <= 1 ? 1 : oInputV;
				oInput.value = oInputV;
				updataCart();
			};
			
			function updataCart(){
				//获取绑定的属性data-id的值
				var goodsId = event.target.dataset.id;
				$.post(api + 'api_cart.php?token='+ localStorage.getItem('token'), {'goods_id':goodsId, 'number':oInputV},function(){
					//找到单价和小计元素
					var unitPrice = parseInt(event.target.parentNode.nextElementSibling.innerHTML);
					var xiaoji = event.target.parentNode.nextElementSibling.nextElementSibling;
					xiaoji.innerHTML = oInputV*unitPrice+'.00';
					//求和
					getAllPrice();	
				});
			};
			
			//点击加号
			if(event.target.innerText == '+'){
				var oInput = event.target.previousElementSibling;
				var oInputV = parseInt(oInput.value);
				oInputV++;
				oInputV = oInputV >= 20 ? 20 : oInputV;
				oInput.value = oInputV;
				updataCart();
			};
			
			//点击全选
			if(event.target.className == 'allBtn'){
				lock = lock ? false : true;
				var strChecked = lock ? 'checked' : "";
				//获取所有的复选框
				var oCheckbox = document.querySelectorAll('.check');
				
				//遍历
				for(var a=0; a<oCheckbox.length;a++){
					oCheckbox[a].checked = strChecked;
					
					//给所有选中的元素加个属性
					if(strChecked == 'checked'){
						oCheckbox[a].setAttribute('name','haha')
					}else{
						oCheckbox[a].setAttribute('name','')
					};
				};
				getAllPrice();
			};
			
			//点击单选
			if(event.target.className == 'check'){
//				console.log(event.target);
				
				if(event.target.checked){
					event.target.setAttribute('name','haha');
				}else{
					event.target.setAttribute('name','');
				};
				//求和
				getAllPrice();
			};
			
			//删除
			if(event.target.className == 'delete'){
				//获取当前点击元素的ID;
				var goodsId = event.target.dataset.id;
				$.post(api+'api_cart.php?token=' + localStorage.getItem('token'), {'goods_id':goodsId,'number':0}, function(result){
					console.log(result);
					if(result.code == 0){
						var oTr = event.target.parentNode.parentNode;
						var oTable = oTr.parentNode;
						oTable.removeChild(oTr);
						
						getAllPrice();
					};
				});
			}
		};
	})();
})();

//写一个求和的方法
function getAllPrice(){
	//先得到所有标记元素
	var oAll = document.querySelectorAll('[name="haha"]');
	var oAllp = document.getElementById("allPrice")
	//遍历
	var Allprice = 0;
	for(var z=0;z<oAll.length;z++){
		var allxiaoji = oAll[z].parentNode.nextElementSibling.nextElementSibling.nextElementSibling.nextElementSibling.innerHTML;
//		console.log(allxiaoji);
		Allprice += parseInt(allxiaoji);
	};
	oAllp.innerHTML = '总价：'+Allprice+'.00'
}
//
/*
 a
 
 * 
 * 
 * a
 * a
 * a
 * a
 * a
 * 
 * a
 * 
 * a
 * a
 * */